package hyh.com.videodemo;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.SystemClock;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.View;
import android.widget.Toast;
import android.widget.VideoView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.databinding.DataBindingUtil;

import com.example.videoplayer.R;
import com.example.videoplayer.databinding.ActivityMainBinding;
import com.tbruyelle.rxpermissions2.RxPermissions;

import java.io.File;

import io.reactivex.functions.Consumer;


public class MainActivity extends AppCompatActivity{

    ActivityMainBinding activityMainBinding;
    SimplePlayer simplePlayer = new SimplePlayer();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_main);
        activityMainBinding = DataBindingUtil.setContentView(this,R.layout.activity_main);


        RxPermissions rxPermissions = new RxPermissions(this);
        rxPermissions.request(Manifest.permission.CAMERA,Manifest.permission.RECORD_AUDIO,Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE)
                .subscribe(grant->{
                    if(grant){
                        activityMainBinding.videoViewRecord.getHolder().addCallback(callBackRecord);
                        activityMainBinding.videoViewPlay.getHolder().addCallback(callbackPlay);
                    }else{
                        Toast.makeText(this,"摄像头无权限",Toast.LENGTH_SHORT).show();
                    }
                });


    }

    /**
     * A native method that is implemented by the 'native-lib' native library,
     * which is packaged with this application.
     */
    public native String stringFromJNI();

    SurfaceHolder.Callback callBackRecord = new SurfaceHolder.Callback() {
        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            Log.e("androidLog","surfaceCreated");
            VideoRecorder.getInstance()
                    .setSurfaceHolder(holder)
                    .setEncodeCallback(encodeCallback)
                    .startRecord();
        }

        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

        }

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            VideoRecorder.getInstance().stopRecord();
        }
    } ;

    VideoRecorder.MediaCallback encodeCallback= new VideoRecorder.MediaCallback() {
        @Override
        public void onCallBack(byte[] data, int len) {
            simplePlayer.putData(data,len);
        }
    };

    SurfaceHolder.Callback callbackPlay = new SurfaceHolder.Callback() {
        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    File file = new File(Environment.getExternalStorageDirectory()+File.separator+"a.mpeg");
//            simplePlayer.start("http://vfx.mtime.cn/Video/2019/03/18/mp4/190318214226685784.mp4",holder.getSurface());
                    simplePlayer.start(file.getAbsolutePath(),holder.getSurface());
                }
            },3000);

        }

        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

        }

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            simplePlayer.stop();
        }
    };


}